package com.example.controller;

import com.example.pojo.Inheritor;
import com.example.service.InheritorService;
import com.example.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@CrossOrigin(origins = "*") // 添加跨域支持
@RequestMapping("/inheritor")
public class InheritorController {
    @Autowired
    private InheritorService inheritorService;

    // 添加获取所有传承人列表的接口
    @GetMapping("/all")
    public List<Inheritor> getInheritors() {
        try {
            List<Inheritor> inheritors = inheritorService.getAllInheritors();
            return inheritors;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // 添加获取单个传承人详情的接口
    @GetMapping("/{id}")
    public Inheritor getInheritorById(@PathVariable Integer id) {
        try {
            return inheritorService.getInheritorById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // 添加根据非遗项目ID获取传承人列表的接口
    @GetMapping("/heritage/{heritageId}")
    public List<Inheritor> getPersonsByHeritageId(@PathVariable Integer heritageId) {
        try {
            return inheritorService.getPersonsByHeritageId(heritageId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // 添加根据传承人类别获取传承人列表的接口
    @GetMapping("/by-category")
    public List<Inheritor> findAllByCategory(
            @RequestParam(required = false) Integer regionId,
            @RequestParam(required = false) Integer speciesId) {
        return inheritorService.findAllByCategory(regionId, speciesId);
    }
    // 增加传承人
    @PostMapping("/add")
    public void addInheritor(@RequestParam("url") MultipartFile url,
                                    @RequestParam("name") String name,
                                    @RequestParam("sex") String sex,
                                    @RequestParam("birthday") LocalDateTime birthday,
                                    @RequestParam("card") String card,
                                    @RequestParam("nation") String nation,
                                    @RequestParam("regionName") String regionName,
                                    @RequestParam("speciesName") String speciesName,
                                    @RequestParam("experience") String experience,
                                    @RequestParam("representativeWorks") String representativeWorks,
                                    @RequestParam("story") String story,
                                    @RequestParam("projectName") String projectName) {
        Inheritor inheritor = new Inheritor();
        inheritor.setName(name);
        inheritor.setSex(sex);
        inheritor.setBirthday(birthday);
        inheritor.setCard(card);
        inheritor.setNation(nation);
        inheritor.setRegionName(regionName);
        inheritor.setSpeciesName(speciesName);
        inheritor.setExperience(experience);
        inheritor.setRepresentativeWorks(representativeWorks);
        inheritor.setStory(story);
        inheritor.setProjectName(projectName);
        String imagePath = null;
        try {
            imagePath = FileUploadUtils.processFileUpload(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        inheritor.setUrl(imagePath);
        inheritorService.insertInheritor(inheritor);
    }

    // 删除传承人
    @DeleteMapping("/{id}")
    public void deleteInheritor(@PathVariable Integer id) {
        inheritorService.deleteInheritorById(id);
    }

    // 更新传承人信息
    @PutMapping("/update/{id}")
    public void updateInheritor(@PathVariable Integer id,
                                      @RequestParam(value = "url",required = false) MultipartFile url,
                                      @RequestParam(value = "name",required = false) String name,
                                      @RequestParam(value = "sex",required = false) String sex,
                                      @RequestParam(value = "birthday",required = false) LocalDateTime birthday,
                                      @RequestParam(value = "card",required = false) String card,
                                      @RequestParam(value = "nation",required = false) String nation,
                                      @RequestParam(value = "regionName",required = false) String regionName,
                                      @RequestParam(value = "speciesName",required = false) String speciesName,
                                      @RequestParam(value = "experience",required = false) String experience,
                                      @RequestParam(value = "representativeWorks",required = false) String representativeWorks,
                                      @RequestParam(value = "story",required = false) String story,
                                      @RequestParam(value = "projectName",required = false) String projectName) {
        Inheritor inheritor = inheritorService.getInheritorById(id);
        if(name != null ){
            inheritor.setName(name);
        }
        if(sex != null ){
            inheritor.setSex(sex);
        }
        if(birthday != null){
            inheritor.setBirthday(birthday);
        }
        if(card != null ){
            inheritor.setCard(card);
        }
        if(nation != null ){
            inheritor.setNation(nation);
        }
        if(regionName != null ){
            inheritor.setRegionName(regionName);
        }
        if(speciesName != null ){
            inheritor.setSpeciesName(speciesName);
        }
        if(experience != null ){
            inheritor.setExperience(experience);
        }
        if(representativeWorks != null){
            inheritor.setRepresentativeWorks(representativeWorks);
        }
        if(story != null){
            inheritor.setStory(story);
        }
        if(projectName != null ) {
            inheritor.setProjectName(projectName);
        }
        if (url != null && !url.isEmpty()) {
            String imagePath = null;
            try {
                imagePath = FileUploadUtils.processFileUpload(url);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            inheritor.setUrl(imagePath);
        }
        inheritorService.updateInheritor(inheritor);
}
}